home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Development Platforms / Apple II / Apple II Sample Code / MPW IIGS SC / SC.021.Dynamo / app.builder / build.a < prev    next >
Encoding:
Text File  |  1990-06-24  |  15.3 KB  |  823 lines  |  [TEXT/MPS ]

  1. ****************************************************************
  2. *                            *
  3. * buildapp.system                        *
  4. *                            *
  5. * Apple II 8-bit application builder and launcher.        *
  6. * Copyright (C) 1990 Apple Computer.            *
  7. * Version 3.1                        *
  8. *                            *
  9. * Written by Eric Soldan, Apple II DTS            *
  10. *                                                              *
  11. * Developer Technical Support Apple II Sample Code             *
  12. *                                                              *
  13. ****************************************************************
  14.  
  15.         include    ':dynamo.includes:sys.equ'
  16.         include    ':dynamo.includes:rt.h'
  17.         include    ':dynamo.includes:rt.macros'
  18.  
  19.         include    'app.config'
  20.  
  21. *********************************************
  22.  
  23. buildText    equ    $1600        ;Script file for building application.
  24.  
  25. fileName        equ    $1E00        ;filestr starts at $1E01.  Runtime
  26.                     ;strings aren't pascal-type.  There
  27.                     ;is a table called strlens that holds
  28.                     ;the lengths.  There is another table
  29.                     ;called maxstrlens that defines how
  30.                     ;big each string can get.
  31.                     ;To make a pascal string for ProDOS,
  32.                     ;the characters for the file name
  33.                     ;are placed in filestr.  Then the
  34.                     ;string length byte is copied to
  35.                     ;fileName, just in front of filestr.
  36.                     ;Now we have a regular pascal string
  37.                     ;for ProDOS.
  38. btFileName    equ    $1E80        ;This is the boot time file name.  If
  39.                     ;the user of BUILDAPP.SYSTEM wants to
  40.                     ;have his text in a file other than
  41.                     ;BUILDAPP.TEXT, or if the script file
  42.                     ;is in another directory, then he can
  43.                     ;place the pathname of the file into
  44.                     ;BUILDAPP.SYSTEM, starting at byte $2006.
  45.                     ;This is the standard way for launched
  46.                     ;programs to be given a pathname.  The
  47.                     ;default pathname in this location
  48.                     ;is BUILDAPP.TEXT. btpathstr starts at
  49.                     ;$1E81.  This string works like filestr,
  50.                     ;in reverse.  The pascal string is first
  51.                     ;copied into btFileName, and then the
  52.                     ;length byte is stored in the strlens
  53.                     ;table.  The reason for copying the
  54.                     ;string from $2006 is because this area
  55.                     ;will be written over, and if the user
  56.                     ;wants to re-run the program due to an
  57.                     ;error, this string would have been lost.
  58.  
  59. *********************************************
  60.  
  61. MLI        equ    $BF00        ;Various equates for ProDOS.
  62. paramCount    equ    $00
  63.  
  64. CREATE        equ    $C0
  65. create_pathname    equ    $01
  66. create_access    equ    $03
  67. create_filetype    equ    $04
  68. create_auxtype    equ    $05
  69. create_strgtype    equ    $07
  70. create_createdate    equ    $08
  71. create_createtime    equ    $0A
  72.  
  73. OPEN        equ    $C8
  74. open_pathname    equ    $01
  75. open_iobuffer    equ    $03
  76. open_refnum    equ    $05
  77.  
  78. READ        equ    $CA
  79. read_refnum    equ    $01
  80. read_databuffer    equ    $02
  81. read_reqcount    equ    $04
  82. read_transcount    equ    $06
  83.  
  84. WRITE        equ    $CB
  85. write_refnum    equ    $01
  86. write_databuffer    equ    $02
  87. write_reqcount    equ    $04
  88. write_transcount    equ     $06
  89.  
  90. SETEOF        equ    $D0
  91. setEOF_refnum    equ    $01
  92. setEOF_EOF    equ    $02
  93.  
  94. SETFILEINFO    equ    $C3
  95. setInfo_pathname    equ    $01
  96. setInfo_access    equ    $03
  97. setInfo_filetype    equ    $04
  98. setInfo_auxtype    equ    $05
  99. setInfo_nullfield    equ    $07
  100. setInfo_moddate    equ    $0A
  101. setInfo_modtime    equ    $0C
  102.  
  103. GETFILEINFO    equ    $C4
  104. getInfo_pathname    equ    $01
  105. getInfo_access    equ    $03
  106. getInfo_filetype    equ    $04
  107. getInfo_auxtype    equ    $05
  108. getInfo_strgtype    equ    $07
  109. getInfo_blksused    equ    $08
  110. getInfo_moddate    equ    $0A
  111. getInfo_modtime    equ    $0C
  112. getInfo_crdate    equ    $0E
  113. getInfo_crtime    equ    $10
  114.  
  115. CLOSE        equ    $CC
  116. close_refnum    equ    $01
  117.  
  118. QUIT        equ    $65
  119.  
  120. *********************************************
  121.  
  122. numstrings    equ    5
  123.  
  124. bffrstr        equ    0
  125. maxbffrstr    equ    127
  126. bffrstrloc    equ    $1D01
  127.  
  128. tempstr        equ    1
  129. maxtempstr    equ    127
  130. tempstrloc    equ    $1D81
  131.  
  132. filestr        equ    2
  133. maxfilestr    equ    127
  134. filestrloc    equ    $1E01
  135.  
  136. btpathstr    equ    3
  137. maxbtpathstr    equ    64
  138. btpathstrloc    equ    $1E81
  139.  
  140. pathstr        equ    4
  141. maxpathstr    equ    127
  142. pathstrloc    equ    $2007
  143.  
  144. *********************************************
  145.  
  146.         export    varspace
  147. varspace        PROC
  148.         ds.b    256
  149.         endp
  150.  
  151. ******************
  152.  
  153.         export    strspace
  154. strspace        PROC
  155.         export    strlens, maxstrlens, strlocs
  156. strlens        ds.b    numstrings
  157. maxstrlens    dc.b    maxbffrstr, maxtempstr, maxfilestr, maxbtpathstr, maxpathstr
  158. strlocs        dc.w    bffrstrloc, tempstrloc, filestrloc, btpathstrloc, pathstrloc
  159.         endp
  160.  
  161. ******************
  162.  
  163. main        PROC
  164.  
  165.         lda    #0        ;Clear the variable space.
  166.         tax            ;This application does not
  167. @clearvars    sta    varspace,x    ;need to variables to be
  168.         inx            ;pre-cleared.
  169.         bne    @clearvars
  170.  
  171.         ldx    #64        ;Get boot path from system
  172. @getPath        lda    $2006,x        ;file and place it in btpathstr.
  173.         sta    btFileName,x
  174.         dex
  175.         bpl    @getPath
  176.         lda    btFileName
  177.         sta    strlens+btpathstr
  178.  
  179. ******************
  180.  
  181. restart        lda #0            ;This string is the last read from the
  182.         sta    strlens+bffrstr    ;buildapp.text file.  Init it to NULL.
  183.  
  184.         jsr    $C300        ;Initialize 80-col screen.
  185.         _writecr
  186.         jsr    home
  187.  
  188.         _rtreset
  189.         _hibitchrs
  190.  
  191.         _write    'IIe Application builder  v3.1',13,\
  192.             'Copyright (C) 1990 by Apple Computer.',13,13,\
  193.             'Building application, please wait.',13,13,13
  194.  
  195.         _strcpy    filestr,btpathstr
  196.                     ;Put file name of load build text
  197.                     ;in filestr.  All proDOS operations
  198.                     ;that need a file name expect it in
  199.                     ;filestr in this application.
  200.  
  201.         jsr    loadFile
  202.         dc.w    buildText
  203.         dc.w    $FFFF
  204.         bcc    @a
  205.         _write    13,13,'Could not load '
  206.         _prstr    filestr
  207.         _write    '.'
  208.         jmp    abort
  209.  
  210.  
  211. @a        lda    readBlock+read_transcount
  212.         sta    ptr
  213.         lda    readBlock+read_transcount+1
  214.         clc
  215.         adc    #>buildText    ;Make sure that the build text
  216.         sta    ptr+1        ;ends with a c/r, or else _readstr
  217.         lda    #13        ;may get real unhappy.
  218.         ldy    #0
  219.         sta    (ptr),y
  220.         iny            ;If a string of 1 char (a 255) is read,
  221.         iny            ;then the end of the file was read
  222.         sta    (ptr),y        ;prematurely, and an error will be issued.
  223.         dey
  224.         lda    #255
  225.         sta    (ptr),y
  226.  
  227.  
  228.         _restore #buildText    ;Point readData at buildText.
  229.         _readend #13        ;_readstr will stop at a c/r.
  230.  
  231.         _set    buildaddr,*$2006+65
  232. * The dispatcher has the ending location stored in this address.  Point after
  233. * dispatcher.  We don't have to load the dispatcher, since it is already at
  234. * $2000.  (It was used to launch this application.)
  235.  
  236.         _var    pathstr        ;pathstr location is $2007, so text
  237.         jsr    getstr        ;is read directly into dispatcher.
  238.         bcc    @ok1
  239.         jsr    EOFMessage
  240.         _write    'pathname.'
  241.         jmp    abort
  242.  
  243.  
  244. @ok1        lda    strlens+pathstr
  245.         sta    $2006        ;Make a pascal string out of it.
  246.  
  247. ******************
  248.  
  249. mainloop        _var    mode
  250.         jsr    getint
  251.         bcc    @ok2
  252.         jsr    EOFMessage
  253.         _write    'bank selection bit setting.'
  254.         jmp    abort
  255.  
  256. @ok2        lda    varspace+mode+1
  257.         bne    @bad2
  258.         lda    varspace+mode
  259.         and    #$87
  260.         cmp    varspace+mode
  261.         beq    @ok2a
  262. @bad2        _write    'Bad bank selection bit setting ('
  263.         _var    mode
  264.         lda    decimalint
  265.         bne    @bad2a
  266.         _write    '$'
  267.         _vhexout
  268.         jmp    @bad2b
  269. @bad2a        _vdecout
  270. @bad2b        _write    ').'
  271.         jmp    abort
  272.  
  273. @ok2a        _var    address
  274.         jsr    getint
  275.         bcc    @ok3
  276.         jsr    EOFMessage
  277.         lda    varspace+mode
  278.         bpl    @aa
  279.         _write    'starting'
  280.         jmp    @ab
  281. @aa        _write    'segment'
  282. @ab        _write    ' address.'
  283.         jmp    abort
  284.  
  285. @ok3        lda    varspace+mode    ;Bit 7 of mode byte indicates no
  286.         bpl    @a        ;more segments.  Following field
  287.         jmp    startaddr    ;is launch address.
  288.  
  289. @a        _var    filestr
  290.         jsr    getstr
  291.         bcc    @ok4
  292.         jsr    EOFMessage
  293.         _write    'filename.'
  294.         jmp    abort
  295.  
  296. @ok4        _write 'Loading '
  297.         _prstr filestr
  298.         _write '...',13
  299.  
  300.         lda    varspace+buildaddr ;Make space for 5 bytes that
  301.         clc            ;will be filled in later.
  302.         adc    #5
  303.         sta    @addr
  304.         lda    varspace+buildaddr+1
  305.         adc    #0
  306.         sta    @addr+1
  307.         jsr    loadFile
  308. @addr        dc.w    $2000
  309.         dc.w    $FFFF
  310.         bcc    @b
  311.         jmp    noLoad
  312.  
  313. @b        lda    varspace+buildaddr ;Put the segment relocation
  314.         sta    ptr        ;information in front of segment.
  315.         lda    varspace+buildaddr+1
  316.         sta    ptr+1
  317.         ldy    #0
  318.         lda    varspace+mode
  319.         sta    (ptr),y
  320.         iny
  321.         lda    varspace+address
  322.         sta    (ptr),y
  323.         iny
  324.         lda    varspace+address+1
  325.         sta    (ptr),y
  326.         iny
  327.         lda    readBlock+read_transcount
  328.         sta    (ptr),y
  329.         iny
  330.         lda    readBlock+read_transcount+1
  331.         sta    (ptr),y
  332.  
  333.         lda    ptr        ;Point past segment.
  334.         clc
  335.         adc    #5
  336.         bcc    @c
  337.         inc    ptr+1
  338.         clc
  339. @c        adc    readBlock+read_transcount
  340.         sta    varspace+buildaddr
  341.         lda    ptr+1
  342.         adc    readBlock+read_transcount+1
  343.         sta    varspace+buildaddr+1
  344.  
  345.         jmp    mainloop        ;Load another segment.
  346.  
  347. startaddr    _write    13,'Starting address is '
  348.         _var    tempstr
  349.         jsr    getstr
  350.         bcc    @ok5
  351.         jsr    crEOFMessage
  352.         _write    'address display format.'
  353.         jmp    abort
  354.  
  355. @ok5        lda    tempstrloc
  356.         _var    address
  357.         cmp    #'$'
  358.         beq    @hex
  359.         _vdecout
  360.         jmp    @a
  361. @hex        _rtcout
  362.         _vhexout
  363. @a        _writecr
  364.  
  365.         lda    varspace+buildaddr ;Store the launch information.
  366.         sta    ptr
  367.         lda    varspace+buildaddr+1
  368.         sta    ptr+1
  369.         ldy    #0
  370.         lda    varspace+mode
  371.         sta    (ptr),y
  372.         iny
  373.         lda    varspace+address
  374.         sta    (ptr),y
  375.         iny
  376.         lda    varspace+address+1
  377.         sta    (ptr),y
  378.  
  379.         lda    ptr        ;Calculate application size.
  380.         sec
  381.         sbc    #<$2000-3
  382.         sta    varspace+applen
  383.         lda    ptr+1
  384.         sbc    #>$2000-3
  385.         sta    varspace+applen+1
  386.  
  387.         _write    13,'Application length is '
  388.         _var    tempstr
  389.         jsr    getstr
  390.         bcc    @ok6
  391.         jsr    crEOFMessage
  392.         _write    'length display format.'
  393.         jmp    abort
  394.  
  395. @ok6        lda    tempstrloc
  396.         _var    applen
  397.         cmp    #'$'
  398.         beq @hex0
  399.         _vdecout
  400.         jmp @a0
  401. @hex0        _rtcout
  402.         _vhexout
  403.  
  404. @a0        _write    13,13,'Save application? (Y,N,Q) '
  405.         _var    tempstr        ;See if we have a script value for
  406.         jsr    getstr        ;Y,N,Q.
  407.         ldx    strlens+tempstr
  408.         beq    @b        ;Nothing, so no Y,N,Q script command.
  409.         lda    tempstrloc
  410.         and    #$5F
  411.         cmp    #'Y'
  412.         beq    @b0
  413.         cmp    #'N'
  414.         beq    @b0
  415.         cmp    #'Q'
  416.         beq    @b0        ;Wasn't a Y,N,Q script command.
  417. @b        jsr    rdkey
  418. @b0        and    #$5F
  419.         cmp    #'Y'
  420.         beq    saveit
  421.         cmp    #'N'
  422.         beq    runit
  423.         cmp    #'Q'
  424.         bne    @b
  425.  
  426. quit        jsr    rtcout
  427.         jsr    MLI
  428.         dc.b    QUIT
  429.         dc.w    @parmTable
  430. @parmTable    dc.b    4
  431.         dc.b    0
  432.         dc.w    0
  433.         dc.b    0
  434.         dc.w    0
  435.  
  436. runit        jsr    rtcout
  437.         jmp    $2000
  438.  
  439. saveit        jsr    rtcout
  440.         _var    filetype
  441.         jsr    getint
  442.         bcc    @ok7
  443.         jsr    crEOFMessage
  444.         _write    'filetype.'
  445.         jmp    abort
  446.  
  447. @ok7        _var    auxtype
  448.         jsr    getint
  449.         bcc    @ok8
  450.         jsr    crEOFMessage
  451.         _write    'auxtype.'
  452.         jmp    abort
  453.  
  454. @ok8        _writecr            ;Go ahead and save it.
  455.         _var    filestr
  456.         jsr    getstr
  457.         bcc    @ok9
  458.         jsr    crEOFMessage
  459.         _write    'application filename.'
  460.         jmp    abort
  461.  
  462. @ok9        _write    13,'Saving application as '
  463.         _prstr
  464.         lda    varspace+applen
  465.         sta    @len
  466.         lda    varspace+applen+1
  467.         sta    @len+1
  468.         jsr    saveFile
  469.         dc.w    $2000
  470. @len        dc.w    $FFFF
  471.         bcc @a
  472.         _write    13,13,'Could not save '
  473.         _prstr    filestr
  474.         _write    '.'
  475.         jmp    abort
  476.  
  477. @a        jsr    getFileInfo    ;Update filetype and auxtype.
  478.         bcc    @b
  479.         _write    13,13,'Could not get file info for '
  480.         _prstr    filestr
  481.         _write    '.'
  482.         jmp    abort
  483.  
  484. @b        jsr    getToSet        ;Move getInfo data to setInfo block.
  485.         lda    varspace+filetype    ;Set filetype and auxtype.
  486.         sta    setFileInfoBlock+setInfo_filetype
  487.         lda    varspace+auxtype
  488.         sta    setFileInfoBlock+setInfo_auxtype
  489.         jsr    setFileInfo
  490.         bcc    @c
  491.         _write    13,13,'Could not set file info for '
  492.         _prstr    filestr
  493.         _write    '.'
  494.         jmp    abort
  495.  
  496. @c        jmp    $2000        ;Launch application.
  497.  
  498.  
  499. noLoad        _write    13,13,'Could not load '
  500.         _prstr    filestr
  501.         _write    '.'
  502.  
  503. abort        _write    13,'Please fix and press any key to rebuild ',\
  504.             '(or Q to quit). '
  505.         jsr    rdkey
  506.         and    #$5F
  507.         cmp    #'Q'
  508.         bne    @a
  509.         jmp    quit
  510. @a        jmp    restart
  511.  
  512.  
  513. getstr        txa
  514.         pha
  515.         jsr    fillbffrstr
  516.         pla
  517.         tax
  518.         bcs    @rts
  519. @ok        _strcpy    ,bffrstr
  520.         lda    #0
  521.         sta    strlens+bffrstr
  522.         clc
  523. @rts        rts
  524.  
  525.  
  526. getint        stx    @xreg
  527.         jsr    fillbffrstr    ;Make sure we have something to chew.
  528.         bcs    @exit        ;If we don't, return an error.
  529.  
  530.         _strval    bffrstr        ;Get the value of the string and
  531.         ldx    @xreg        ;put it in the variable.
  532.         _set
  533.         _var    bffrstr
  534.         ldy    #0
  535.         sty    decimalint
  536. @a        tya            ;First skip over -'s.
  537.         _strchr
  538.         iny
  539.         cmp    #'-'
  540.         beq    @a
  541.         cmp    #'$'
  542.         beq    @b
  543.         dec    decimalint    ;It was a decimal integer.
  544. @b        ldy    strvalcount
  545. @c        tya
  546.         cmp    strlens,x
  547.         bcs    @d        ;Ran out of string.
  548.         _strchr
  549.         iny
  550.         cmp    #','
  551.         bne    @c
  552.         tya
  553. @d        _midstrcpy ,bffrstr
  554.         clc            ;All went well.
  555. @exit        ldx    @xreg        ;Restore x-reg.
  556.         rts            ;All done
  557. @xreg        dc.b    0
  558. decimalint    dc.b    0
  559.  
  560.  
  561. fillbffrstr    lda    strlens+bffrstr    ;See if there is any string left.
  562.         beq    @a        ;There is not, so get another.
  563.         clc
  564.         rts            ;There is, so munch away.
  565. @a        _readstr    bffrstr        ;Read in the next.
  566.         jsr    stripComment    ;Chew off comments -- (who needs them?)
  567.         lda    strlens+bffrstr    ;How did we do?
  568.         beq    @a        ;Empty line -- try again.
  569.         lda    bffrstrloc    ;Check for eof marker.
  570.         cmp    #255
  571.         rts            ;Return carry set if eof hit.
  572.  
  573.  
  574. stripComment    txa
  575.         pha
  576.         asl    a
  577.         tax
  578.         lda    strlocs,x    ;Point to the string.
  579.         sta    ptr
  580.         lda    strlocs+1,x
  581.         sta    ptr+1
  582.         pla
  583.         tax
  584.         ldy    #0
  585. @a        tya
  586.         cmp    strlens,x
  587.         beq    @b        ;Hit end-of-string.
  588.         lda    (ptr),y
  589.         iny
  590.         cmp    #';'
  591.         bne    @a        ;Not at comment yet.
  592.         dey
  593.  
  594. @b        dey            ;Remove all trailing white-space.
  595.         bpl    @c        ;Still some characters left.
  596.         lda    #0
  597.         sta    strlens,x    ;Ran out of string.
  598.         rts
  599.  
  600. @c        lda    (ptr),y        ;See if it white-space.
  601.         cmp    #9
  602.         beq    @b        ;If it is, keep backing up.
  603.         cmp    #' '
  604.         beq    @b
  605.         iny            ;We ran into something solid.
  606.         tya
  607.         sta    strlens,x
  608.  
  609. @noComment    rts
  610.  
  611. crEOFMessage    _writecr
  612. EOFMessage    _write 'Build script EOF hit while reading '
  613.         rts
  614.  
  615. *********************************************
  616.  
  617.  
  618. createFile    jsr    MLI
  619.         dc.b    CREATE
  620.         dc.w    createBlock
  621.         rts
  622. createBlock    dc.b    7
  623.         dc.w    fileName
  624.         dc.b    $C3
  625.         dc.b    $06
  626.         dc.w    $2000
  627.         dc.b    $01
  628.         dc.w    $00
  629.         dc.w    $00
  630.  
  631.  
  632. setFileInfo    jsr    MLI
  633.         dc.b    SETFILEINFO
  634.         dc.w    setFileInfoBlock
  635.         rts
  636. setFileInfoBlock    dc.b    7
  637.         dc.w    fileName
  638.         dc.b    0
  639.         dc.b    0
  640.         dc.w    0
  641.         ds.b     3
  642.         dc.w    0
  643.         dc.w    0
  644.  
  645.  
  646. getFileInfo    jsr    MLI
  647.         dc.b    GETFILEINFO
  648.         dc.w    getFileInfoBlock
  649.         rts
  650. getFileInfoBlock    dc.b    10
  651.         dc.w    fileName
  652.         dc.b    0
  653.         dc.b    0
  654.         dc.w    0
  655.         ds.b    1
  656.         dc.w    1
  657.         dc.w    0
  658.         dc.w    0
  659.         dc.w    0
  660.         dc.w    0
  661.  
  662.  
  663. getToSet        ldy    #13-1
  664. @a        lda    getFileInfoBlock+getInfo_pathname,y
  665.         sta    setFileInfoBlock+getInfo_pathname,y
  666.         dey
  667.         bpl    @a
  668.         rts
  669.  
  670.  
  671. openFile        jsr    MLI
  672.         dc.b    OPEN
  673.         dc.w    openBlock
  674.         ldx    openBlock+open_refnum
  675.         stx    readBlock+read_refnum
  676.         stx    writeBlock+write_refnum
  677.         stx    setEOFBlock+setEOF_refnum
  678.         stx    closeBlock+close_refnum
  679.         rts
  680. openBlock    dc.b    3
  681.         dc.w    fileName
  682.         dc.w    $BF00-1024
  683.         dc.b    1
  684.  
  685.  
  686. readFile        jsr    MLI
  687.         dc.b    READ
  688.         dc.w    readBlock
  689.         rts
  690. readBlock    dc.b    4
  691.         dc.b    1
  692.         dc.w    $2000
  693.         dc.w    $2000
  694.         dc.w    0
  695.  
  696.  
  697. writeFile    jsr    MLI
  698.         dc.b    WRITE
  699.         dc.w    writeBlock
  700.         rts
  701. writeBlock    dc.b    4
  702.         dc.b    1
  703.         dc.w    $2000
  704.         dc.w    $2000
  705.         dc.w    0
  706.  
  707.  
  708. setEOF        jsr    MLI
  709.         dc.b    SETEOF
  710.         dc.w    setEOFBlock
  711.         rts
  712. setEOFBlock    dc.b    2
  713.         dc.b    1
  714.         ds.b    3
  715.  
  716.  
  717. closeFile    jsr    MLI
  718.         dc.b    CLOSE
  719.         dc.w    closeBlock
  720.         rts
  721. closeBlock    dc.b    1
  722.         dc.b    1
  723.  
  724.  
  725. loadFile        pla            ;Set ptr to point to params.
  726.         sta    ptr
  727.         pla
  728.         sta    ptr+1
  729.         jsr    prepFile        ;Get all data ready.
  730.         lda    ptr+1        ;Restore return address.
  731.         pha
  732.         lda    ptr
  733.         pha
  734.         jsr    openFile        ;Try opening the file.
  735.         bcs    @rts        ;Bad news...
  736.         jsr    readFile        ;Try reading the file.
  737.         bcc    @a        ;Good news.
  738.         php            ;Read failed -- try closing the
  739.         pha            ;file and restoring the error
  740.         jsr    closeFile    ;to the read error status.
  741.         pla
  742.         plp
  743.         rts
  744. @a        jsr    closeFile    ;Return result of close.
  745. @rts        rts
  746.  
  747.  
  748. saveFile        pla            ;Set ptr to point to params.
  749.         sta    ptr
  750.         pla
  751.         sta    ptr+1
  752.         jsr    prepFile        ;Get all data ready.
  753.         lda    ptr+1        ;Restore return address.
  754.         pha
  755.         lda    ptr
  756.         pha
  757.         jsr    createFile    ;Try creating file.
  758.         bcc    @a        ;There was no such file.
  759.         cmp    #$47        ;Make sure error is duplicate file error.
  760.         sec
  761.         bne    @rts
  762.  
  763. @a        jsr    openFile        ;Try opening the file.
  764.         bcs    @rts        ;Bad news...
  765.  
  766.         jsr    writeFile    ;Try writing the file.
  767.         bcc    @b        ;Good news.
  768.         php            ;Write failed -- try closing the
  769.         pha            ;file and restoring the error
  770.         jsr    closeFile    ;to the write error status.
  771.         pla
  772.         plp
  773.         rts
  774.  
  775. @b        lda    writeBlock+write_reqcount
  776.         sta    setEOFBlock+setEOF_EOF
  777.         lda    writeBlock+write_reqcount+1
  778.         sta    setEOFBlock+setEOF_EOF+1
  779.         lda    #0
  780.         sta    setEOFBlock+setEOF_EOF+2
  781.         jsr    setEOF
  782.         bcc    @c
  783.         php
  784.         pha
  785.         jsr    closeFile
  786.         pla
  787.         plp
  788.         rts
  789.  
  790. @c        jsr    closeFile    ;Return result of close.
  791. @rts        rts
  792.  
  793.  
  794. prepFile        lda    strlens+filestr
  795.         sta    fileName        ;Length of string.
  796.         ldy    #1
  797.         lda    (ptr),y
  798.         sta    readBlock+read_databuffer
  799.         sta    writeBlock+write_databuffer
  800.         iny
  801.         lda    (ptr),y
  802.         sta    readBlock+read_databuffer+1
  803.         sta    writeBlock+write_databuffer+1
  804.         iny
  805.         lda    (ptr),y
  806.         sta    readBlock+read_reqcount
  807.         sta    writeBlock+write_reqcount
  808.         iny
  809.         lda    (ptr),y
  810.         sta    readBlock+read_reqcount+1
  811.         sta    writeBlock+write_reqcount+1
  812.         tya
  813.         clc
  814.         adc    ptr
  815.         sta    ptr
  816.         bcc    @d
  817.         inc    ptr+1
  818. @d        rts
  819.  
  820.         endp
  821.  
  822.         END
  823.